<?php

/**
 * This is the model class for table "moja_baza.osoba".
 *
 * The followings are the available columns in table 'moja_baza.osoba':
 * @property integer $idOsoby
 * @property string $imie
 * @property string $nazwisko
 * @property string $pesel
 *
 * The followings are the available model relations:
 * @property Uzytkownik[] $uzytkowniks
 */
class Osoba extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Osoba the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'moja_baza.osoba';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('pesel', 'required'),
			array('idOsoby', 'numerical', 'integerOnly'=>true),
			array('imie', 'length', 'max'=>15),
			array('nazwisko', 'length', 'max'=>20),
                        array('pesel', 'length', 'min'=>11, 'max'=>11),
                        array('pesel', 'checksum'),
                        array('pesel', 'ifExists', 'on'=>'register'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idOsoby, imie, nazwisko, pesel', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'uzytkowniks' => array(self::HAS_MANY, 'Uzytkownik', 'idOsoby'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idOsoby' => 'Id Osoby',
			'imie' => 'Imię',
			'nazwisko' => 'Nazwisko',
			'pesel' => 'PESEL',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idOsoby',$this->idOsoby);
		$criteria->compare('imie',$this->imie,true);
		$criteria->compare('nazwisko',$this->nazwisko,true);
		$criteria->compare('pesel',$this->pesel,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * Calculates checksum for PESEL number.
	 * This is the 'checksum' validator as declared in rules().
	 */
	public function checksum($attribute,$params=NULL)
	{
		if(!$this->hasErrors($attribute) && strlen($this->pesel) == 11)
		{
			$pesel = str_split($this->pesel);
                        $checksum = 0;
                        $n = array(1, 3, 7, 9, 1, 3, 7, 9, 1, 3);
                        for ($i=0; $i<=9; $i++) {
                            $checksum += $n[$i] * $pesel[$i];
                        }
                        if ((10-($checksum%10))%10 != $pesel[10])
                            $this->addError('pesel','Numer jest nieprawidłowy.');
                }
	}
        
        public function ifExists($attribute,$params=NULL)
        {
                if(!$this->hasErrors($attribute) && strlen($this->pesel) == 11)
		{
                        $data=Osoba::model()->findByAttributes(array('pesel'=>$this->pesel));
                        if (!$data) {
				Yii::app()->user->setState('uzytkownikScenario', 'notAllowed');
				$this->addError('pesel',
                                    'Podany numer PESEL nie jest uprawniony do założenia konta.');
                                return "not";
                        } else {
				$data2=Uzytkownik::model()->findByAttributes(array('idOsoby'=>$data->idOsoby));
                                if (!$data2) {
					Yii::app()->user->setState('uzytkownikScenario', 'newUzytkownik');
					return "new";
				} else {
					Yii::app()->user->setState('uzytkownikScenario', 'oldUzytkownik');	
					$this->addError('pesel',
                                            'Podany numer PESEL nie jest uprawniony do założenia konta.');
                                        return "old";
				}
			}
                }
        }

	public function getFullname()
	{
			return $this->imie.' '.$this->nazwisko;
	}
}